package com.bana.fvs.app.module.detaile;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.common.adapter.CommonAdapter;
import com.bana.fvs.app.common.adapter.ViewHolder;
import com.bana.fvs.app.listener.AdapterListener;
import com.bana.fvs.app.main.adapter.FogLampAdapter;
import com.bana.fvs.app.main.view.CommonHeadRefreshView;
import com.bana.fvs.app.main.view.MyItemDecoration;
import com.bana.fvs.app.module.detaile.entity.FoglightDetailInfo;
import com.bana.fvs.app.module.tab2.entity.ControlResultBean;
import com.bana.fvs.app.module.tab2.tab2Detail.entity.PopWindowItemBean;
import com.bana.fvs.app.mvp.ConstantsValue;
import com.bana.fvs.app.mvp.contract.FogLampListContract;
import com.bana.fvs.app.mvp.presenter.FogLampPresenter;
import com.bana.fvs.app.other.xrecyclerview.ProgressStyle;
import com.bana.fvs.app.other.xrecyclerview.XRecyclerView;
import com.bana.fvs.app.util.TimeUtils;
import com.bana.fvs.app.util.UserInfoUtils;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.DisplayUtil;
import com.wei.commonlibrary.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wei on 2018/4/1.
 * 雾灯列表
 */

public class FogLampListActivity extends BaseActivity<FogLampPresenter> implements FogLampListContract.IFogLampView {

    @BindView(R.id.recyclerView)
    XRecyclerView recyclerView;
    private CommonHeadRefreshView mCommonHeadRefreshView;

    private static String EXTRA_KZQBM = "kzqbm";

    private String kzqbm;
    private int pagenum;

    private boolean mIsRefresh, mIsLoadmore;

    private List<FoglightDetailInfo> mFoglightDetailInfos;
    private FogLampAdapter mFogLampAdapter;

    public static void startAction(Context context, String kzqbm) {
        Intent intent = new Intent(context, FogLampListActivity.class);
        intent.putExtra(EXTRA_KZQBM, kzqbm);
        context.startActivity(intent);
    }


    @Override
    public int setLayout() {
        return R.layout.activity_fog_lamp;
    }

    @Override
    public void setTitle() {
        mBaseTitleBarView.setTitleText(mContext.getString(R.string.tab_title_main3));
        mBaseTitleBarView.setLeftTitleGone();
        mBaseTitleBarView.setLeftDrawable(-1);
    }

    /**
     * 处理返回键
     */
    @Override
    public void onTitleLeftPressed() {
        onBackPressed();
    }

    @Override
    public void initWidget() {
        kzqbm = getIntent().getStringExtra(EXTRA_KZQBM);
        View headView = findViewById(R.id.commonHeadViewId);
        mCommonHeadRefreshView = new CommonHeadRefreshView(mContext, headView, "");


        if (null != mCommonHeadRefreshView) {
            mCommonHeadRefreshView.setITimeRefresh(() -> http_getFogLampList());
        }

        mFoglightDetailInfos = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setPullRefreshEnabled(true);//下拉刷新-可以
        recyclerView.setLoadingMoreEnabled(false);//上拉加载-可以
        recyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);//设置下拉刷新的样式
        recyclerView.addItemDecoration(new MyItemDecoration());
        mFogLampAdapter = new FogLampAdapter(mFoglightDetailInfos);
        recyclerView.setAdapter(mFogLampAdapter);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mIsRefresh = true;
                pagenum = 0;
                http_getFogLampList();
            }

            @Override
            public void onLoadMore() {
                pagenum += 1;
                mIsLoadmore = true;
                http_getFogLampList();
            }
        });

        mFogLampAdapter.setOnItemClickListener((position, foglightDetailInfo) -> {
            int kzqbm = foglightDetailInfo.getKzqbm();
           // EquipmentDetailActivity.startAction(FogLampListActivity.this, 2, foglightDetailInfo.getWdbm() + "", kzqbm + "");

            EquipmentDetailActivity.startAction(FogLampListActivity.this, 2,mFoglightDetailInfos.get(position) );
        });

        /**
         * 右边的HandleICON点击事件
         */
        mFogLampAdapter.setOnHandleBtnClickListener((position, view) -> showPopupwindow(mFoglightDetailInfos.get(position), view));
        http_getFogLampList();
    }

    private void http_getFogLampList() {
        mPresenter.getFogLampList(this, UserInfoUtils.getInstance().getToken(this), kzqbm, pagenum);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    protected void initPresenter() {
        mPresenter.setView(this);
    }

    @Override
    public void onError(CommException e) {
        loadDataComplete();
        String message = getString(R.string.hint_get_list_error);
        if (TextUtils.isEmpty(e.getMessage())) {
            message = e.getMessage();
        }
        ToastUtil.showShort(getApplicationContext(), message);
    }

    private void loadDataComplete() {
        if (mIsRefresh) {
            mIsRefresh = false;
            recyclerView.refreshComplete();
        }else if(mIsLoadmore){
            mIsLoadmore = false;
            recyclerView.loadMoreComplete();
        }
    }

    @Override
    public void onResponse(List<FoglightDetailInfo> foglightDetailInfos) {
        loadDataComplete();
        if (mIsRefresh) {
            mFoglightDetailInfos.clear();
            recyclerView.refreshComplete();
        }
        if (mIsLoadmore) {
            recyclerView.loadMoreComplete();
        }

        mFoglightDetailInfos.addAll(foglightDetailInfos);
        mFogLampAdapter.notifyDataSetChanged();

        mIsRefresh = false;
        mIsLoadmore = false;
        mCommonHeadRefreshView.refreshData(TimeUtils.getCurrentTime());
    }


    private void showPopupwindow(FoglightDetailInfo foglightDetailInfo, View anchorView) {
        final PopupWindow popupWindow = new PopupWindow(mContext);
        popupWindow.setFocusable(true);
        int width = 0, height = 0;
        height = DisplayUtil.dp2px(mContext, 220);
        width = DisplayUtil.dp2px(mContext, 110);
        popupWindow.setWidth(width);
        popupWindow.setHeight(height);
        //popupWindow.setAnimationStyle(R.style.anim_menu_bottombar);//设置动画效果
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        View view = View.inflate(mContext, R.layout.layout_pop_view, null);
        ListView listView = view.findViewById(R.id.popListView);
        List<PopWindowItemBean> datas = new ArrayList<>();

        /**
         *----------------------------------------------------------------------------------------------------
         */
        PopWindowItemBean bean = new PopWindowItemBean();
        bean.setPopName(getResources().getString(R.string.mandatory_yellow_light));
        bean.setPopCode(ConstantsValue.CONTROL_STATUS_YELLO);
        bean.setPopLeftDrawable(R.drawable.pop_yellow);
        datas.add(bean);
        PopWindowItemBean bean1 = new PopWindowItemBean();
        bean1.setPopName(getResources().getString(R.string.mandatory_red_light));
        bean1.setPopCode(ConstantsValue.CONTROL_STATUS_RED);
        bean1.setPopLeftDrawable(R.drawable.pop_red);
        datas.add(bean1);
        PopWindowItemBean bean2 = new PopWindowItemBean();
        bean2.setPopName(getResources().getString(R.string.check_red_light));
        bean2.setPopCode(ConstantsValue.CONTROL_STATUS_VEHRED);
        bean2.setPopLeftDrawable(R.drawable.pop_car_charge);
        datas.add(bean2);
        PopWindowItemBean bean3 = new PopWindowItemBean();
        bean3.setPopName(getResources().getString(R.string.foglight_close));
        bean3.setPopCode(ConstantsValue.CONTROL_STATUS_CLOSE);
        bean3.setPopLeftDrawable(R.drawable.pop_close);
        datas.add(bean3);
        PopWindowItemBean bean4 = new PopWindowItemBean();
        bean4.setPopName(getResources().getString(R.string.foglight_start));
        bean4.setPopCode(ConstantsValue.CONTROL_STATUS_OPEN);
        bean4.setPopLeftDrawable(R.drawable.pop_open);
//        datas.add(bean4);
        PopWindowItemBean bean5 = new PopWindowItemBean();
        bean5.setPopName(getResources().getString(R.string.foglight_restart));
        bean5.setPopCode(ConstantsValue.CONTROL_STATUS_RESTART);
        bean5.setPopLeftDrawable(R.drawable.pop_restart);
        datas.add(bean5);

        /**
         *----------------------------------------------------------------------------------------------------
         */
        CommonAdapter<PopWindowItemBean> adapter;
        adapter = new CommonAdapter<PopWindowItemBean>(datas, R.layout.item_layout_pop_lv_item) {
            @Override
            public void convert(ViewHolder holder, PopWindowItemBean item) {
                LinearLayout ll_container = holder.getView(R.id.item_ll_container);
                ll_container.setGravity(Gravity.LEFT);
                ll_container.setPadding(DisplayUtil.dp2px(mContext, 10), ll_container.getPaddingTop(), 0, ll_container.getPaddingBottom());
                //文字
                if (!TextUtils.isEmpty(item.getPopName()))
                    holder.setText(R.id.popTextView, item.getPopName());
                //左边ICON
                if (item.getPopLeftDrawable() != -1)
                    holder.setImageResource(R.id.popImageView, item.getPopLeftDrawable());
            }
        };

        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view1, position1, id) -> {
            PopWindowItemBean bean0 = datas.get(position1);
            String kzcz = bean0.getPopCode();//控制操作
            int sbbm = 0;
            if (foglightDetailInfo != null) {
                sbbm = foglightDetailInfo.getSbbm();
            }
            ToastUtil.showLong(mContext, bean0.toString());
            //雾灯修改
            mPresenter.http_controlLamp(this, UserInfoUtils.getInstance().getToken(mContext), String.valueOf(sbbm), kzcz);
            popupWindow.dismiss();
        });
        popupWindow.setContentView(view);
        int[] locations = new int[2];
        anchorView.getLocationOnScreen(locations);//获取当前控件在屏幕上的位置
        popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, locations[0] - width + anchorView.getWidth(), locations[1] + anchorView.getHeight());
    }

    @Override
    public void onControllerResult(ControlResultBean bean) {
        if (bean.isKzzt()) {
            ToastUtil.showShort(mContext, getString(R.string.tip_toast_update_success));
        } else {
            ToastUtil.showShort(mContext, getString(R.string.tip_toast_update_error));
        }
    }
}
